Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # Copyright (C) 2014 Kouhei Sutou <kou@clear-code.com> | ||
| # | ||
| # This library is free software; you can redistribute it and/or | ||
| # modify it under the terms of the GNU Lesser General Public | ||
| # License as published by the Free Software Foundation; either | ||
| # version 2.1 of the License, or (at your option) any later version. | ||
| # | ||
| # This library is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| # Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public | ||
| # License along with this library; if not, write to the Free Software | ||
| # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
|
|
||
| module ChupaText | ||
| # Attributes of data. | ||
| class Attributes < Struct.new(:title, | ||
| :author, | ||
| :encoding, | ||
| :created_time, | ||
| :modified_time) | ||
| include Enumerable | ||
|
|
||
| def initialize | ||
| super | ||
| @members = members | ||
| @extra_data = {} | ||
| end | ||
|
|
||
| def to_h | ||
| super.merge(@extra_data) | ||
| end | ||
|
|
||
| def inspect | ||
| super.gsub(/>\z/) do | ||
| " @extra_data=#{@extra_data.inspect}>" | ||
| end | ||
| end | ||
|
|
||
| # @yield [name, value] Gives attribute name and value to the block. | ||
| # @yieldparam [Symbol] name The attribute name. | ||
| # @yieldparam [Object] value The attribute value. | ||
| def each(&block) | ||
| if block.nil? | ||
| Enumerator.new(self, :each) | ||
| else | ||
| each_pair(&block) | ||
| @extra_data.each_pair(&block) | ||
| end | ||
| end | ||
|
|
||
| # Gets the value of attribute named `name`. | ||
| # | ||
| # @param [Symbol, String] name The attribute name. | ||
| # @return [Object] The attribute value. | ||
| def [](name) | ||
| name = normalize_name(name) | ||
| if @members.key?(name) | ||
| super | ||
| else | ||
| @extra_data[name] | ||
| end | ||
| end | ||
|
|
||
| # Sets `value` as the value of attribute named `name`. | ||
| # | ||
| # @param [Symbol, String] name The attribute name. | ||
| # @param [Object] value The attribute value. | ||
| def []=(name, value) | ||
| name = normalize_name(name) | ||
| if @members.key?(name) | ||
| send("#{name}=", value) | ||
| else | ||
| @extra_data[name] = value | ||
| end | ||
| end | ||
|
|
||
| # Sets `encoding` as the `encoding` attribute value. | ||
| # | ||
| # @param [String, Encoding, nil] encoding The encoding. | ||
| def encoding=(encoding) | ||
| super(normalize_encoding(encoding)) | ||
| end | ||
|
|
||
| # Sets `time` as the `created_time` attribute value. | ||
| # | ||
| # @param [String, Integer, Time, nil] time The created time. | ||
| # If `time` is `Integer`, it is used as UNIX time. | ||
| def created_time=(time) | ||
| super(normalize_time(time)) | ||
| end | ||
|
|
||
| # Sets `time` as the `modified_time` attribute value. | ||
| # | ||
| # @param [String, Integer, Time, nil] time The modified time. | ||
| # If `time` is `Integer`, it is used as UNIX time. | ||
| def modified_time=(time) | ||
| super(normalize_time(time)) | ||
| end | ||
|
|
||
| private | ||
| def normalize_name(name) | ||
| name.to_sym | ||
| end | ||
|
|
||
| def normalize_encoding(encoding) | ||
| if encoding.is_a?(String) | ||
| encoding = Encoding.find(encoding) | ||
| end | ||
| encoding | ||
| end | ||
|
|
||
| def normalize_time(time) | ||
| case time | ||
| when String | ||
| Time.parse(time) | ||
| when Integer | ||
| Time.at(time).utc | ||
| else | ||
| time | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # Copyright (C) 2014 Kouhei Sutou <kou@clear-code.com> | ||
| # | ||
| # This library is free software; you can redistribute it and/or | ||
| # modify it under the terms of the GNU Lesser General Public | ||
| # License as published by the Free Software Foundation; either | ||
| # version 2.1 of the License, or (at your option) any later version. | ||
| # | ||
| # This library is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| # Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public | ||
| # License along with this library; if not, write to the Free Software | ||
| # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
|
|
||
| class TestAttributes < Test::Unit::TestCase | ||
| def setup | ||
| @attributes = ChupaText::Attributes.new | ||
| end | ||
|
|
||
| sub_test_case("title") do | ||
| def test_accessor | ||
| assert_nil(@attributes.title) | ||
| @attributes.title = "Title" | ||
| assert_equal("Title", @attributes.title) | ||
| end | ||
| end | ||
|
|
||
| sub_test_case("author") do | ||
| def test_accessor | ||
| assert_nil(@attributes.author) | ||
| @attributes.author = "Alice" | ||
| assert_equal("Alice", @attributes.author) | ||
| end | ||
| end | ||
|
|
||
| sub_test_case("encoding") do | ||
| def test_string | ||
| @attributes.encoding = "UTF-8" | ||
| assert_equal(Encoding::UTF_8, @attributes.encoding) | ||
| end | ||
|
|
||
| def test_encoding | ||
| @attributes.encoding = Encoding::UTF_8 | ||
| assert_equal(Encoding::UTF_8, @attributes.encoding) | ||
| end | ||
|
|
||
| def test_nil | ||
| @attributes.encoding = nil | ||
| assert_nil(@attributes.encoding) | ||
| end | ||
| end | ||
|
|
||
| sub_test_case("created_time") do | ||
| def test_string | ||
| @attributes.created_time = "2014-02-17T23:14:30+09:00" | ||
| assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), | ||
| @attributes.created_time) | ||
| end | ||
|
|
||
| def test_integer | ||
| @attributes.created_time = 1392646470 | ||
| assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), | ||
| @attributes.created_time) | ||
| end | ||
|
|
||
| def test_time | ||
| @attributes.created_time = Time.parse("2014-02-17T23:14:30+09:00") | ||
| assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), | ||
| @attributes.created_time) | ||
| end | ||
|
|
||
| def test_nil | ||
| @attributes.created_time = nil | ||
| assert_nil(@attributes.created_time) | ||
| end | ||
| end | ||
|
|
||
| sub_test_case("modified_time") do | ||
| def test_string | ||
| @attributes.modified_time = "2014-02-17T23:14:30+09:00" | ||
| assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), | ||
| @attributes.modified_time) | ||
| end | ||
|
|
||
| def test_integer | ||
| @attributes.modified_time = 1392646470 | ||
| assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), | ||
| @attributes.modified_time) | ||
| end | ||
|
|
||
| def test_time | ||
| @attributes.modified_time = Time.parse("2014-02-17T23:14:30+09:00") | ||
| assert_equal(Time.parse("2014-02-17T23:14:30+09:00"), | ||
| @attributes.modified_time) | ||
| end | ||
|
|
||
| def test_nil | ||
| @attributes.modified_time = nil | ||
| assert_nil(@attributes.modified_time) | ||
| end | ||
| end | ||
| end |